package com.ruoyi.web.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pinyin.PinYinUtils;
import com.ruoyi.web.domain.mes.MesPaper;
import com.ruoyi.web.mapper.MesConfigMapper;
import com.ruoyi.web.mapper.MesPaperMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.MesWarehousingDetail;
import com.ruoyi.web.mapper.MesWarehousingInfoMapper;
import com.ruoyi.web.domain.MesWarehousingInfo;
import com.ruoyi.web.service.IMesWarehousingInfoService;

import javax.annotation.Resource;


/**
 * 入库基本信息Service业务层处理
 *
 * @author wxt
 * @date 2024-01-15
 */
@Service
public class MesWarehousingInfoServiceImpl implements IMesWarehousingInfoService {
    @Resource
    private MesWarehousingInfoMapper mesWarehousingInfoMapper;

    @Resource
    private MesPaperMapper mesPaperMapper;
    @Resource
    private MesConfigMapper configMapper;
    /**
     * 查询入库基本信息
     *
     * @param id 入库基本信息主键
     * @return 入库基本信息
     */
    @Override
    public MesWarehousingInfo selectMesWarehousingInfoById(Long id) {
        MesWarehousingInfo d = mesWarehousingInfoMapper.selectMesWarehousingInfoById(id);
        return d;
    }

    /**
     * 查询入库基本信息列表
     *
     * @param mesWarehousingInfo 入库基本信息
     * @return 入库基本信息
     */
    @Override
    public List<MesWarehousingInfo> selectMesWarehousingInfoList(MesWarehousingInfo mesWarehousingInfo) {
        return mesWarehousingInfoMapper.selectMesWarehousingInfoList(mesWarehousingInfo);
    }

    /**
     * 查询纸张入库明细
     *
     * @param Detail 入库基本信息
     * @return 入库基本信息
     */
    @Override
    public List<MesWarehousingDetail> selectMesDetaillist(MesWarehousingDetail Detail) {
        List<MesWarehousingDetail> d = mesWarehousingInfoMapper.selectMesDetaillist(Detail);
        BigDecimal decimal = mesWarehousingInfoMapper.selectMesDetaillistSumTotal(Detail);
        if(d.size() > 0){
            d.get(0).setTotalSum(decimal);
        }
        return d;
    }

    @Override
    public List<MesWarehousingDetail> selectMesDetaillistExport(MesWarehousingDetail Detail) {
        List<MesWarehousingDetail> d = mesWarehousingInfoMapper.selectMesDetaillistExport(Detail);
        BigDecimal decimal = mesWarehousingInfoMapper.selectMesDetaillistSumTotal(Detail);
        if(d.size() > 0){
            d.get(0).setTotalSum(decimal);
        }
        return d;
    }

    /**
     * 新增入库基本信息
     *
     * @param mesWarehousingInfo 入库基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesWarehousingInfo(MesWarehousingInfo mesWarehousingInfo) {
        mesWarehousingInfo.setCreateTime(DateUtils.getNowDate());
        // 生成入库单编号
//        int maxNum = mesWarehousingInfoMapper.getWarehousingInfoMaxId();

        Map<String, Object> map = new HashMap<>();
        map.put("columnName", "warehousing_code");
        map.put("tableName", "mes_warehousing_info");
        int maxNum = configMapper.getMaxId(map);

        String code = PinYinUtils.getCodeNormal(maxNum, "ZZRK");
        mesWarehousingInfo.setWarehousingCode(code);
        int rows = mesWarehousingInfoMapper.insertMesWarehousingInfo(mesWarehousingInfo);
        insertMesWarehousingDetail(mesWarehousingInfo);
        return rows;
    }

    /**
     * 修改入库基本信息
     *
     * @param mesWarehousingInfo 入库基本信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesWarehousingInfo(MesWarehousingInfo mesWarehousingInfo) {
        String username = SecurityUtils.getUsername();
        mesWarehousingInfo.setUpdateTime(DateUtils.getNowDate());
        mesWarehousingInfo.setUpdateBy(username);

        int status = mesWarehousingInfo.getStatus();
        if (status == 2) {//提交,修改状态用的同一个接口,所以要判断
            MesWarehousingInfo Info = mesWarehousingInfoMapper.selectMesWarehousingInfoById(mesWarehousingInfo.getId());
            List<MesWarehousingDetail> mesWarehousingDetailList = Info.getMesWarehousingDetailList();
            for (MesWarehousingDetail mesWarehousingDetail : mesWarehousingDetailList) {
                updateQuantity(mesWarehousingDetail.getPaperid(), mesWarehousingDetail.getTotal(), 1);
            }
        } else {
            mesWarehousingInfoMapper.deleteMesWarehousingDetailByWarehousingid(mesWarehousingInfo.getId());
            insertMesWarehousingDetail(mesWarehousingInfo);
        }

        return mesWarehousingInfoMapper.updateMesWarehousingInfo(mesWarehousingInfo);
    }

    /**
     * 批量删除入库基本信息
     *
     * @param ids 需要删除的入库基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesWarehousingInfoByIds(Long[] ids) {
        mesWarehousingInfoMapper.deleteMesWarehousingDetailByWarehousingids(ids);//删除入库详情
//        for (long id : ids){
//            MesWarehousingInfo Info= mesWarehousingInfoMapper.selectMesWarehousingInfoById(id);
//            mesWarehousingInfoMapper.deleteMesWarehousingDetailByWarehousingid(id);//删除入库详情
//            List<MesWarehousingDetail> mesWarehousingDetailList = Info.getMesWarehousingDetailList();
//            for (MesWarehousingDetail mesWarehousingDetail : mesWarehousingDetailList)
//            {
//                updateQuantity(mesWarehousingDetail.getPaperid(),mesWarehousingDetail.getTotal(),2);
//            }
//        }
        return mesWarehousingInfoMapper.deleteMesWarehousingInfoByIds(ids);
    }

    /**
     * 删除入库基本信息信息
     *
     * @param id 入库基本信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesWarehousingInfoById(Long id) {
        //MesWarehousingInfo Info= mesWarehousingInfoMapper.selectMesWarehousingInfoById(id);
        mesWarehousingInfoMapper.deleteMesWarehousingDetailByWarehousingid(id);

//        List<MesWarehousingDetail> mesWarehousingDetailList = Info.getMesWarehousingDetailList();
//        for (MesWarehousingDetail mesWarehousingDetail : mesWarehousingDetailList)
//        {
//            updateQuantity(mesWarehousingDetail.getPaperid(),mesWarehousingDetail.getTotal(),2);
//        }
        return mesWarehousingInfoMapper.deleteMesWarehousingInfoById(id);
    }

    /**
     * 新增入库详细信息信息
     *
     * @param mesWarehousingInfo 入库基本信息对象
     */
    public void insertMesWarehousingDetail(MesWarehousingInfo mesWarehousingInfo) {
        List<MesWarehousingDetail> mesWarehousingDetailList = mesWarehousingInfo.getMesWarehousingDetailList();
        Long id = mesWarehousingInfo.getId();
        if (StringUtils.isNotNull(mesWarehousingDetailList)) {
            List<MesWarehousingDetail> list = new ArrayList<MesWarehousingDetail>();
            for (MesWarehousingDetail mesWarehousingDetail : mesWarehousingDetailList) {
                mesWarehousingDetail.setWarehousingid(id);
                list.add(mesWarehousingDetail);
            }
            if (list.size() > 0) {
                mesWarehousingInfoMapper.batchMesWarehousingDetail(list);
            }
        }
    }

    /**
     * 修改纸张信息库存数量
     *
     * @param id 物料id
     * @param number 入库数量
     * @param type 1:加，2:减
     * @return 结果
     */
    private void updateQuantity(Long id, BigDecimal number, int type) {
        MesPaper paper = new MesPaper();
        paper.setId(id);
        paper.setQuantity(number); // 只是借用这个字段，传的是增减量
        paper.setQuantitymark(type);
        mesPaperMapper.updateMesPaperQuantity(paper);
    }

}
